import Base from "./base.js";
import moment from "moment";
import reg from "./reg.js"
import isReg from "./isReg.js"
class Common extends Base {
    constructor() {
        super();
    }
    /**
     * 生成uuid();
     */
    uuid() {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";
        var uuid = s.join("");
        return uuid;
    }
    /**
     * 倒计时，秒为单位
     */
    countDown(s, callback) {
        if (!this.isNum(s)) {
            return;
        }
        var _S = s;
        var Interval = setInterval(function() {
            if (_S < 1) {
                clearInterval(Interval);
                callback(_S, "stop");
                return;
            }
            _S--;
            callback(_S);
        }, 1000);
        return Interval;
    }
    /**
     * setLocalStorage：读，写
     * @param {Stirng} localKey // 缓存Key
     * @param {String} id // 数据ID
     * @param {Object} data // 要存入的数据
     * @param {Number} outMinute // 过期时间：不传为永久；
     */
    setLocalStorage(localKey, id, data, outMinute) {
        if (this.isEmpty(localKey) || this.isEmpty(id)) {
            console.log("localStorage：【localKey】||【id】为空");
            return;
        }
        if (this.isEmptyObj(data)) {
            console.log("localStorage：【data】值请传入对象");
        }
        /**
         * 缓存主KEY
         */
        const LOCAL_KEY = "CACHE_" + localKey;
        /**
         * 数据ID
         */
        const DATA_ID = id;
        /**
         * 缓存数据
         */
        let CACHE_DATA = this.clone(data);
        /**
         * 设置过期时间，当传入为空时，设置null永不过期不考虑清浏览器缓存；
         */
        CACHE_DATA.expirationDate = this.isEmpty(outMinute) ? null : moment().add(outMinute, "minutes").format('x');
        /**
         * 取整个localhost的值
         */
        let LOCAL_DATA = localStorage.getItem(LOCAL_KEY);
        if (this.isEmpty(LOCAL_DATA)) {
            /**
             * 当该值为空 || 不为对象时直接创建或者覆盖
             */
            let CREATE_DATA = {};
            CREATE_DATA[DATA_ID] = CACHE_DATA;
            localStorage.setItem(LOCAL_KEY, JSON.stringify(CREATE_DATA));
        } else {
            /**
             * 当有值时，取出现有数据；
             */
            let UPDATE_DATA = JSON.parse(LOCAL_DATA);
            /**
             * 设置更新的数据
             */
            UPDATE_DATA[DATA_ID] = CACHE_DATA;
            /**
             * 存入缓存
             */
            localStorage.setItem(LOCAL_KEY, JSON.stringify(UPDATE_DATA));
        }
    }
    /**
     * getLocalStorage 读取
     * 
     */
    getLocalStorage(localKey, id) {
        if (this.isEmpty(localKey) || this.isEmpty(id)) {
            console.log("getLocalStorage：【localKey】||【id】为空");
            return;
        }
        /**
         * 缓存主KEY
         */
        const LOCAL_KEY = "CACHE_" + localKey;
        /**
         * 数据ID
         */
        const DATA_ID = id;
        /**
         * 取整个localhost的值
         */
        let LOCAL_DATA = localStorage.getItem(LOCAL_KEY);
        if (this.isEmpty(LOCAL_DATA)) {
            console.log("getLocalStorage：" + LOCAL_KEY + "为空");
            return null;
        }
        /**
         * 读取缓存
         */
        let GET_DATA = JSON.parse(LOCAL_DATA);
        if (this.isEmpty(GET_DATA[DATA_ID])) {
            console.log("getLocalStorage：" + LOCAL_KEY + "." + DATA_ID + "为空")
            return null;
        }
        /**
         * 当有时间时 || 当数据小于当前时间时进行删除，并且更新数据库
         */
        if (!this.isEmpty(GET_DATA[DATA_ID].expirationDate) && GET_DATA[DATA_ID].expirationDate < moment().format('x')) {
            delete GET_DATA[DATA_ID];
            localStorage.setItem(LOCAL_KEY, JSON.stringify(GET_DATA));
            return null;
        }
        /**
         * 返回当前数据
         */
        return GET_DATA[DATA_ID];
    }
    /**
     * 两个数组之间的差集
     */
    difference(arr1, arr2) {
        let diff = [];
        let tmp = arr2;
        arr1.forEach((val1, i) => {
            if (arr2.indexOf(val1) < 0) {
                diff.push(val1);
            } else {
                tmp.splice(tmp.indexOf(val1), 1);
            }
        });
        return diff.concat(tmp)
    }
    /**
     * OBJECT,ARRAY 深克龙
     */
    clone(_v) {
        if (this.isEmptyArr(_v)) {
            return [];
        }
        if (this.isEmptyObj(_v)) {
            return {};
        }
        return JSON.parse(JSON.stringify(_v))
    }
    /**
     * Array to Tree
     */
    toTree(data, key, parent) {
        if (!this.isArr(data)) {
            return;
        }
        if (this.isEmpty(parent) || !this.isStr(parent) || this.isEmpty(key) || !this.isStr(key)) {
            return;
        }
        let tree = [];
        let list = data;
        let obj = {};
        list.map(item => {
            obj[item[key]] = item;
        });
        list.map(item => {
            if (obj[item[parent]]) {
                if (this.isEmpty(obj[item[parent]]["children"])) {
                    obj[item[parent]]["children"] = [];
                }
                obj[item[parent]]["children"].push(item);
            } else {
                tree.push(item)
            }
        });
        return tree;
    }
    /**
     * 遍历所有上级 树形结构
     * @param {valueKey} 取值
     */
    toAllParent(data, id, key, parent, valueKey) {
        let valuekey = valueKey || key;
        if (!this.isArr(data)) {
            return;
        }
        if (this.isEmpty(parent) || this.isEmpty(key)) {
            return;
        }
        let parents = [];
        let list = data;
        let obj = {};
        list.map(item => {
            obj[item[key]] = item;
        });
        // 递归
        let recursion = (Pid) => {
            if (this.isObj(obj[Pid])) {
                parents.push(obj[Pid][valuekey]);
                recursion(obj[Pid][parent]);
            }
        }
        recursion(id);
        return parents.reverse();
    }
}

let common = new Common();
let VueCommon = {
    install(Vue, option) {
        Vue.prototype.$common = common;
        Vue.prototype.$reg = reg;
        Vue.prototype.$isReg = isReg;
    }
}
export {
    VueCommon,
    common,
    reg,
    isReg
}